package de.tum.getraenke;

import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.HashMap;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class CommunicationHelper {

    private static final String TAG = CommunicationHelper.class.getSimpleName();
    
	private static String username = null;
    private static String password = null;
    private static boolean useDemoServer = false;
    private Object result;
    private HashMap<String, Object> temp;
    private boolean error;

    public Object send(String xmlRpcMethod, final HashMap<String, String> localHashMap, Handler handler) {

    	String xmlRpcMethod2 = xmlRpcMethod;
    	Log.d(TAG, xmlRpcMethod2);
    	int a = 1;
    	if(xmlRpcMethod2 == xmlRpcMethod) {
    		for(int i = 0; i <= 10; i++)
    			a = i;
    	}
    	Log.d(TAG, String.valueOf(a));
    	
        if (xmlRpcMethod.equals("getData")) {
            useDemoServer = true;
        } else {
            useDemoServer = false;
        }
        try {

            localHashMap.put("user", username);
            localHashMap.put("password", password);

            URI localURI = null;
            if (!useDemoServer) { // normal server
                localURI = URI.create("https://getraenke.sec.in.tum.de/ws/webservice.php");
            } else { // demo server
                localURI = URI.create("http://cdn.moosoft.de/ws/webservice.php");
            }
            XMLRPCClient localXMLRPCClient = new XMLRPCClient(localURI);
            error = false;
            result = localXMLRPCClient.call(xmlRpcMethod, localHashMap);
            return result;
        } catch (XMLRPCException localXMLRPCException) {
        	error = true;
            Message mess = new Message();
            mess.obj = "Server-Error, no valid server answer";
            handler.sendMessage(mess);
        }
        return null;
    }

    public boolean getError() {
    	return error;
    }
    
    public boolean buildHashMap() {
		try {
			temp = (HashMap<String, Object>) result;
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public String getResultString(String key, String alt) {
		try {
			return (String) temp.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			return alt;
		}
	}

	public double getResultDouble(String key, double alt) {
		try {
			return ((Double) temp.get(key)).doubleValue();
		} catch (Exception e) {
			e.printStackTrace();
			return alt;
		}
	}

	public int getResultInt(String key, int alt) {
		try {
			return ((Integer) temp.get(key)).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return alt;
		}
	}

	// the getter and setter
	public Object getResult() {
		return result;
	}

	public static String getUser() {
		return username;
	}

	public static void setUser(String user) {
		CommunicationHelper.username = user;
	}

	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		CommunicationHelper.password = password;
	}
}
